#include "server.h"
#include <iostream>
#include <csignal>
#include <cstdlib>

volatile sig_atomic_t running = 1;

void signal_handler(int sig) {
    std::cout << "\n接收到信号 " << sig << "，正在停止服务器..." << std::endl;
    running = 0;
}

int main(int argc, char* argv[]) {
    // 设置信号处理
    std::signal(SIGINT, signal_handler);
    std::signal(SIGTERM, signal_handler);

    int port = 8080;
    if (argc > 1) {
        port = std::atoi(argv[1]);
    }

    try {
        Server server;
        std::cout << "===========================================" << std::endl;
        std::cout << "🎓 学生选课系统服务器启动" << std::endl;
        std::cout << "===========================================" << std::endl;
        std::cout << "端口: " << port << std::endl;
        std::cout << "访问地址: http://localhost:" << port << std::endl;
        std::cout << "===========================================" << std::endl;
        std::cout << "测试账号:" << std::endl;
        std::cout << "管理员: admin / admin123" << std::endl;
        std::cout << "教师: teacher001 / teacher123" << std::endl;
        std::cout << "学生: student001 / student123" << std::endl;
        std::cout << "学生: student002 / student123" << std::endl;
        std::cout << "===========================================" << std::endl;
        std::cout << "按 Ctrl+C 停止服务器" << std::endl;
        std::cout << "===========================================" << std::endl;

        server.run(port);

    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
        return 1;
    }

    std::cout << "服务器已停止" << std::endl;
    return 0;
}